package com.warehouse.controller;


import com.warehouse.entity.po.AddColour;
import com.warehouse.entity.po.DeleteColourPO;
import com.warehouse.entity.po.PagePO;
import com.warehouse.entity.po.UpColour;
import com.warehouse.entity.pojo.Colour;
import com.warehouse.result.Result;
import com.warehouse.service.impl.ColourServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 颜色 前端控制器
 * </p>
 *
 * @author 卓汉杰
 * @since 2021-05-07
 */
@Api("颜色管理")
@CrossOrigin
@RestController
@RequestMapping("/colour")
public class ColourController {

    @Autowired
    ColourServiceImpl colourService;


    @ApiOperation(value = "添加颜色")
    @PostMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody AddColour addColour){
        Colour colour1 = new Colour();
        colour1.setName(  addColour.getColour());

        boolean save = colourService.save(colour1);

        if(save){
            return new Result(null,Result.OK,"添加成功");
        }{
            return new Result(null,Result.NULL,"添加失败");
        }

    }


    @ApiOperation(value = "通过id获取颜色")
    @GetMapping("/getById")
    @ResponseBody
    public Result getById( Integer  id){
        Colour colour = colourService.getById(id);

        if (colour == null) {
            return new Result(null,Result.NULL,"该颜色不存在");
        }else {
           Map<String,Object> data = new HashMap<>();
           data.put("name",colour.getName());
           return new Result(data,Result.OK,"请求成功");
        }


    }

    @ApiOperation(value = "获取颜色列表")
    @GetMapping("/listPage")
    @ResponseBody
    public Result listPage(PagePO listPageColourPO){

        com.baomidou.mybatisplus.extension.plugins.pagination.Page page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(listPageColourPO.getPageNum(),listPageColourPO.getPageSize());

        com.baomidou.mybatisplus.extension.plugins.pagination.Page page1 = colourService.page(page);

        Map data = new HashMap();
        data.put("total",page1.getTotal());
        data.put("size",page1.getSize());
        data.put("colour",page1.getRecords());
        return new Result(data,Result.OK,"请求成功");
    }


    @ApiOperation(value = "修改颜色")
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody UpColour upColour){
        Colour colour1 = new Colour();
        colour1.setId(upColour.getId());
        colour1.setName(  upColour.getColour());

        boolean save = colourService.updateById(colour1);

        if(save){
            return new Result(null,Result.OK,"修改成功");
        }{
            return new Result(null,Result.NULL,"修改失败");
        }

    }


    @ApiOperation(value = "根据id删除颜色")
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody DeleteColourPO deleteColourPO){


        boolean save = colourService.removeById(deleteColourPO.getId());

        if(save){
            return new Result(null,Result.OK,"删除成功");
        }{
            return new Result(null,Result.NULL,"删除失败");
        }

    }






}

